<!DOCTYPE html>

<html class="no-js">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title></title>
  <meta name="description" content="" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />

  <script src="https://cdn.staticfile.org/axios/0.19.0-beta.1/axios.js"></script>

  <script src="https://cdn.staticfile.org/vue/2.5.17/vue.js"></script>

  <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.13.0/theme-chalk/index.css" />
  <script src="https://cdn.staticfile.org/element-ui/2.13.0/index.js"></script>
  <link rel="stylesheet" href="../../../lib/index.css" />
  <script src="../../../lib/avue.js"></script>
</head>

<body>
  <div id="app">
    <avue-tree ref="tree" :root-count="3" :child-count="3" :filter-node-method="filterNodeMethod" :permission="getPermission" :option="option"
      :data="data" @node-click="nodeClick" @update="update" @save="save" @del="del" v-model="form" :is-formate-data="false" :is-auto-first-click="false" :draggable="true">
      <span class="el-tree-node__label" slot-scope="{ node, data }">
        <span>
          <i class="el-icon-user-solid"></i>
          {{ node.label }}
        </span>
      </span>
      <template slot-scope="scope" slot="menu">
        <div class="avue-tree__item" @click.native="tip(scope.node,scope.data)">自定义按钮</di>
      </template>
    </avue-tree>

  </div>
</body>
<script>
  new Vue({
    el: '#app',
    data() {
      return {
        form: {},
        // data:[{
        //     value: 1,
        //   pid: 0,
        //   label: 'body'
        // }, {
        //     value: 2,
        //   pid: 1,
        //   label: 'title'
        // }, {
        //     value: 3,
        //   pid: 2,
        //   label: 'div'
        // }],
        data: [
          {
            value:6,
            pid:0,
            label: '一级部门',
            sort:0,
            ancestor:"",
            children: [
              {
                value: 1,
                pid:6,
                sort:0,
                ancestor:"6",
                children:[],
                label: '一级部门1',
              }, {
                value: 2,
                sort:1,
            ancestor:"6",
                pid:6,
                children:[],
                label: '一级部门2',
              }
            ]
          }, {
            value: 3,
            pid:0,
            sort:1,
            ancestor:"",
            label: '二级部门',
            children: [
              {
                value: 4,
                pid:3,
                sort:0,
            ancestor:"3",
                label: '二级部门1',
                children:[]
              }, {
                value: 5,
                sort:1,
               ancestor:"3",
                pid:3,
                label: '二级部门2',
                children:[]
              },
            //   {
            //     value: 33,
            //     sort:2,
            //    ancestor:"3",
            //     pid:3,
            //     label: '二级部门3',
            //     children:[]
            //   }
            ]
          },
          {
            value:9,
            pid:0,
            label: '三级部门',
            sort:2,
            ancestor:"",
          }
        ],
        option: {
          defaultExpandAll: true,
          dialogWidth: '50%',
          title: "新增部门",
          formOption: {
            labelWidth: 100,
            column: [{
              label: '部门名称',
              prop: 'label'
            }],
          },
          props: {
            labelText: '标题',
            label: 'label',
            value: 'value',
            parent:"pid",
            children: 'children'
          }
        }
      }
    },
    methods: {
      getPermission(key, data) {
       // if (key == "addBtn") { return false; }
        return true;
      },
      tip(node, data) {
        this.$message.success(JSON.stringify(data))
      },
      del(data, done) {
        this.$message.success('删除回调')
        done();
      },
      update(data,done) {
          console.log('data: ', data);
        //console.log(parent);
        this.$message.success('更新回调')
        done(data);
      },
      save(data,done) {
        
        //this.$message.success('新增回调')
        //new Date().getTime()
        data.value = new Date().getTime();
        console.log(data);
        done(data);
      },
      filterNodeMethod(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      nodeClick(data) {
        this.$message.success(JSON.stringify(data))
      }
    }
  })
</script>

</html>